package com.cl.unipay4j.demo.example;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.cl.unipay4j.ali.AliPayConfig;
import com.cl.unipay4j.ali.AliPayFactoryProvider;
import com.cl.unipay4j.ali.AliPayProperties;
import com.cl.unipay4j.core.*;
import com.cl.unipay4j.core.pay.IPayment;
import com.cl.unipay4j.core.pay.PayModel;
import com.cl.unipay4j.core.transfer.ITransfer;
import com.cl.unipay4j.core.transfer.TransferModel;
import com.cl.unipay4j.core.transfer.TransferToAccountModel;
import com.cl.unipay4j.wx.WxPayFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @ClassName PayController
 * @Description
 * @Author Small
 * @Date 2019/11/29 10:52
 * @Version 1.0
 **/
@RestController
@RequestMapping("pay")
@Slf4j
public class PayController {


    @Autowired
    private AliPayProperties aliPayProperties;
    @Autowired
    private AliPayFactoryProvider aliPayFactoryProvider;

    @PostMapping("")
    public R pay(HttpServletRequest request, @RequestBody OrderPayDto dto) {


        AliPayConfig ali = aliPayProperties.getConfig();

        System.out.println(ali.getAppid());

        PayChannel payChannel = dto.getPayChannel();

        // payment
        IPayment payment = getPayment(dto.getTradeType(), payChannel);


        IPayment payment1 = aliPayFactoryProvider.getPayment(dto.getTradeType());

        // 发起支付
        Result payResult = raisePay(payment1);
        return ResultUtil.success(payResult);
    }


    @PostMapping("transfer")
    public R transfer(HttpServletRequest request, @RequestBody OrderPayDto dto) {
        WxPayFactory wxPayFactory = new WxPayFactory();
        ITransfer transfer = wxPayFactory.getTransfer();

        TransferToAccountModel model = new TransferToAccountModel();
        model.setIdentity("oPhr90I8W0GZrTftjJ934UuNg_1Y");
        model.setAmount(new BigDecimal("0.01"));
        model.setOutTradeNo(DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN));
        model.setTitle("测试");
        model.setRemark("测试");

        transfer.execTransferToAccount(model);

       return ResultUtil.success();
    }

    /**
     * 方法描述: 唤起支付
     * @Author Small
     * @Date 2020/2/17 18:15
     * @return com.weixing.mall.pay.PayResult
     */
    private com.cl.unipay4j.core.Result<Object> raisePay(IPayment payment) {
        PayModel payOrder = new PayModel();
        payOrder.setOutTradeNo("202007150000001");
        payOrder.setBody("测试支付");
        payOrder.setSpbillCreateIp("182.119.38.93");
        payOrder.setPrice(new BigDecimal("0.01"));
        return payment.pay(payOrder);
    }

    /**
     * 方法描述: 生成payment
     * @Author Small
     * @param tradeType
     * @param payChannel
     * @Date 2020/2/17 18:14
     * @return com.weixing.mall.pay.IPayment
     */
    private IPayment getPayment(String tradeType, PayChannel payChannel) {
        IPayFactory payFactory = PayFactory.getFactory(payChannel);
        IPayment payment = payFactory.getPayment(tradeType);
        return payment;
    }


}
